package com.hwl.acl.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hwl.acl.pojo.Role;
import com.hwl.acl.service.RoleService;
import com.hwl.commonutils.result.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author long
 * @since 2021-10-16
 */
@Api(tags = "后台角色管理")
@RestController
@RequestMapping("/admin/acl/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @ApiOperation("获取角色分页列表")
    @GetMapping("{limit}/{size}")
    public RespBean index(@PathVariable Long limit, @PathVariable Long size,
                          @RequestParam(value = "roleName", required = false) String roleName) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(roleName)) {
            wrapper.like("role_name", roleName);
        }
        return RespBean.ok().data(roleService.page(new Page<>(limit, size), wrapper));
    }

    @ApiOperation("查看角色")
    @GetMapping("get/{id}")
    public RespBean get(@PathVariable String id) {
        return RespBean.ok().data(roleService.getById(id));
    }

    @ApiOperation("新增角色")
    @PostMapping("save")
    public RespBean save(@RequestBody Role role) {
        if (roleService.save(role)) {
            return RespBean.ok().message("新增角色成功！");
        }
        return RespBean.error().message("新增角色失败！");
    }

    @ApiOperation("修改角色")
    @PutMapping("update")
    public RespBean updateById(@RequestBody Role role) {
        if (roleService.updateById(role)) {
            return RespBean.ok().message("修改角色成功！");
        }
        return RespBean.error().message("修改角色失败！");
    }

    @ApiOperation("删除角色")
    @DeleteMapping("remove/{id}")
    public RespBean remove(@PathVariable String id) {
        if (roleService.removeById(id)) {
            return RespBean.ok().message("删除角色成功！");
        }
        return RespBean.error().message("删除角色失败！");
    }

    @ApiOperation("批量删除角色")
    @DeleteMapping("batchRemove")
    public RespBean batchRemove(@RequestBody List<String> idList) {
        if (roleService.removeByIds(idList)) {
            return RespBean.ok().message("批量删除角色成功！");
        }
        return RespBean.error().message("批量删除角色失败！");
    }

}

